package com.jie.jie.PermissionDispatcher;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.jaydenxiao.common.commonutils.LogUtils;
import com.jaydenxiao.common.commonutils.ToastUitl;
import com.jie.jie.R;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;

@RuntimePermissions
public class DanActivity extends AppCompatActivity {
    /**
     * 1.@RuntimePermissions 在Activity或者Fragment中需要添加，来处理权限的问题 
     2.需要权限的时候，先要检查是否拥有该权限 (MyDeatilsActivity 替换成当前的类(Activity或者Fragment)) 
     3.将需要权限的操作定义在一个方法里，并用 @NeedsPermission(Manifest.permission.CAMERA)表明需要的权限(可以是多个)
     4.编译一下，就会生成【当前类名+PermissionsDispatcher】的类，在原本调用的地方调用@NeedsPermission标记的方法，
     这时候你会发现会对应生成【方法名+WithCheck】的方法
     如：MyDeatilsActivityPermissionsDispatcher.showContactsWithCheck(MyDeatilsActivity.this);
     * @param savedInstanceState
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dan);
    }

    /**
     *
     * 编译后生成
     * 当前类名+PermissionsDispatch.callPhoneWithCheck()
     * 自定义WithCheck之前的方法
     * @param view
     */
    public void click(View view) {
        DanActivityPermissionsDispatcher.callPhoneWithCheck(this);
    }

    /**
     * 需要的权限
     */
    @NeedsPermission(Manifest.permission.CAMERA)
    void callPhone() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivity(intent);
//        Toast.makeText(this, "申请权限", Toast.LENGTH_SHORT).show();
    }

    /**
     * 授权提示回调
     * 先显示这个方法,之后弹出系统弹框
     *
     * @param request
     */
    @OnShowRationale(Manifest.permission.CAMERA)
    void showRationaleForCamera(PermissionRequest request) {
        showRationaleDialog("使用此功能需要打开照相机的权限", request);
    }

    /**
     * 申请权限被拒绝之后执行
     */
    @OnPermissionDenied(Manifest.permission.CAMERA)
    void onCameraDenaid() {
        LogUtils.loge("拒绝了camera权限");
        ToastUitl.showShort("拒绝了camera权限");
    }

    /**
     * 申请权限被拒绝并不在提示时
     */
    @OnNeverAskAgain(Manifest.permission.CAMERA)
    void onCameraNeverAsk() {
        LogUtils.loge("不在询问");
        ToastUitl.showShort("不在询问");
    }


    /**
     * 回调
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        DanActivityPermissionsDispatcher.onRequestPermissionsResult(this,requestCode,grantResults);
    }

    private void showRationaleDialog(String messageResId, final PermissionRequest request) {
        new AlertDialog.Builder(this)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(@NonNull DialogInterface dialog, int which) {
                        request.proceed();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(@NonNull DialogInterface dialog, int which) {
                        request.cancel();
                    }
                })
                .setCancelable(false)
                .setMessage(messageResId)
                .show();
    }
}
